//
//  PushStrategy.m
//  HYHOME
//
//  Created by cjy on 16/3/3.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "PushStrategy.h"
#import "NetWorkHandler.h"
#import "ModelS.h"
#import "PushStrategyCell.h"
#import "Push2Strategy.h"
#import "DKNightVersion.h"
@interface PushStrategy ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *arr;
@property (nonatomic,retain)NSString *strpage;
@property (nonatomic,retain)UITableView *tableView;
@end

@implementation PushStrategy

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor =[UIColor whiteColor];

    self.strpage = [NSString stringWithFormat: @"{\"page_index\":0,\"page_size\":5,\"device_id\":\"311FB630-A637-4803-95BA-2AEC74486504\",\"user_id\":\"0\",\"label_info_list\":[{\"category_name\":\"施工\",\"label_ids\":%ld,\"category_id\":12}]}}",(long)self.article_id];
    [self handle];
    [self createtableView];
}
- (void)handle{
    
    self.arr = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:@"http://tuku-wap.m.jia.com/v1.2.4/hybrid/article/search-by-type"];
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod= @"POST";
    
    request.HTTPBody = [self.strpage dataUsingEncoding:NSUTF8StringEncoding];
    
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        // 进入主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            NSError *errorJSON = nil;
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorJSON];
            

            NSArray *arr = [result objectForKey:@"records"];
            for (NSDictionary *dic in arr) {
                
                ModelS *model = [[ModelS alloc]init];
                
                model.title = [dic objectForKey:@"title"];
                model.cover_image = [dic objectForKey:@"cover_image"];
                model.article_id = [dic objectForKey:@"article_id"];
                
                [self.arr addObject:model];
            }
            
            [self.tableView reloadData];
        });
        
        
        
        
    }];
    
    
    [task resume];
    
    
    


}
-(void)createtableView{

    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    


    [self.tableView registerClass:[PushStrategyCell class] forCellReuseIdentifier:@"pool"];

    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView.showsVerticalScrollIndicator = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arr.count;


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    PushStrategyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    ModelS *model= [self.arr objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    cell.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
    cell.textLabel.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);

    [cell passModel:model];
    return cell;





}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    Push2Strategy *push =[[Push2Strategy alloc]init];
    
    
    ModelS *model = [self.arr objectAtIndex:indexPath.row];
    
    
    push.ida = [model.article_id integerValue];
        
    
    

    [self.navigationController pushViewController:push animated:YES];


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 250;



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
